<?php

    namespace Sale\Controller;

    use Common\Controller\AdminbaseController;

    class AdminGoodController extends AdminbaseController {

        protected $m_sale,$m_sale_good;

        function _initialize() {
            parent::_initialize();
            $this->m_sale= D("Logistics/Sale");
            $this->m_sale_good= D("Sale/SaleGood");
        }

        /**
         * 商品管理
         */
        public function index(){
            $sale_id = get_sale_id();
            $where = array("sale_id"=>$sale_id);
            $count=$this->m_sale_good->where($where)->count();
            $page = $this->page($count, 15);
            $list = $this->m_sale_good->where($where)
                ->order("create_time")
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }
        /**
         * 商品设置
         */
        public function add(){

            $id = I("id");
            if(!empty($id)){
                $item=$this->m_sale_good->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }


        // 商品添加
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = I("post.");
                if(empty($id)){
                    $insert["sale_id"] = get_sale_id();
                    $result =$this->m_sale_good->add($insert);
                }else{
                    $result = $this->m_sale_good->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        // 商品删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_sale_good->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

    }